package main

import (
	"fmt"
	"log"
	"strings"
	"time"

	"gitee.com/dhcy/tencent_ddns/conf"
	"gitee.com/dhcy/tencent_ddns/console"
	"gitee.com/dhcy/tencent_ddns/model"
	"gitee.com/dhcy/tencent_ddns/xlog"
)

func init() {
	log.SetFlags(log.LstdFlags | log.Lshortfile | log.Ldate)
}
func main() {
	err := console.Init()
	if err != nil {
		xlog.Error(fmt.Errorf("init console err:%v", err))
		return
	}
	// 10分钟查看一次
	ticker := time.NewTicker(10 * time.Minute)
	for range ticker.C {
		// 获取公网IP
		publicIp, err := model.GetPublicIp()
		if err != nil {
			xlog.Error(err)
			return
		}
		sd := strings.Split(conf.SubDomain, ";")
		for i := range sd {
			domain := model.NewDomain(conf.Token, conf.Domain, sd[i])
			err = domain.UpdateDomainIp(publicIp)
			if err != nil {
				xlog.Error(err)
				return
			}
		}
	}
	ticker.Stop()
}
